package net.sunxiaolong.tool.captureinsureknowledge.tool.capture;

import net.sunxiaolong.tool.captureinsureknowledge.entity.PiccKnowledge;
import net.sunxiaolong.tool.captureinsureknowledge.repository.PiccKnowledgeRepository;
import net.sunxiaolong.tool.captureinsureknowledge.tool.NetworkTool;
import net.sunxiaolong.tool.captureinsureknowledge.tool.SnowFlakeIdGenerator;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 晓龙
 * @create 2018-08-17 上午9:53
 * @desc
 **/
public class PiccCapture {

    public static void captureBaoXianChanPinZhiShi(String href, PiccKnowledgeRepository piccKnowledgeRepository) throws XPatherException {
        String childPageBaseUrl = href.substring(0, href.lastIndexOf("/"));
        TagNode thisPageTn = new HtmlCleaner().clean(NetworkTool.getStringFromUrl(href, "GB2312"));
        String divPathStr = "//div[@class='iq_content_c_title_A']";
        Object[] divObjArray = thisPageTn.evaluateXPath(divPathStr);
        List<PiccKnowledge> pkList = new ArrayList<>();
        for (Object divObj : divObjArray) {
            TagNode divTn = (TagNode) divObj;
            String onclickStr = divTn.getAttributeByName("onclick");
            String childPageUrl = onclickStr.replaceAll("window.location.href='", "").replaceAll("';", "");

            String contentPageContent = NetworkTool.getStringFromUrl(childPageBaseUrl + "/" + childPageUrl, "GB2312");
            TagNode contentPageTn = new HtmlCleaner().clean(contentPageContent);

            String titleDivPathStr = "//div[@class='iq_content_c_con_middle_c_p_s_title']";
            String contentDivPathStr = "//div[@id='getHeight']";
            TagNode titleDivTn = (TagNode) contentPageTn.evaluateXPath(titleDivPathStr)[0];
            TagNode contentDivTn = (TagNode) contentPageTn.evaluateXPath(contentDivPathStr)[0];
            String title = titleDivTn.getText().toString().replaceAll("&nbsp;", " ").trim();
            String content = contentDivTn.getText().toString().replaceAll("&nbsp;", " ").trim();
            PiccKnowledge pk = new PiccKnowledge(SnowFlakeIdGenerator.nextId(), title, content, childPageBaseUrl + "/" + childPageUrl);
            pkList.add(pk);
        }
        piccKnowledgeRepository.saveAll(pkList);
    }

    public static void captureJiBenGaiNian(String href, PiccKnowledgeRepository piccKnowledgeRepository) throws XPatherException {
        TagNode pageTn = new HtmlCleaner().clean(NetworkTool.getStringFromUrl(href, "GB2312"));
        String divPathStr = "//div[@class='iq_content_c']";
        Object[] divObjArray = pageTn.evaluateXPath(divPathStr);
        List<PiccKnowledge> pkList = new ArrayList<>();
        for (Object divObj : divObjArray) {
            TagNode divTn = (TagNode) divObj;
            TagNode[] childTagArray = divTn.getChildTags();
            String question = childTagArray[0].getText().toString().replaceAll("&nbsp;", " ").trim();
            String answer = childTagArray[1].getText().toString().replaceAll("&nbsp;", " ").trim();
            PiccKnowledge pk = new PiccKnowledge(SnowFlakeIdGenerator.nextId(), question, answer, href);
            pkList.add(pk);
        }
        piccKnowledgeRepository.saveAll(pkList);
    }
}
